// pages/canprint/canprint.js
Page({

  data: {   code:'',flag:false,url:'',last:''

  },

  onLoad: function (options) {
    this.setData({code:options.code,url:options.url})

  },
    canprint(){
        this.setData({flag:true})
         if(this.flag==true){
           return
         }
         console.log(222);
         
        this.throttle(()=>{console.log(111)
          wx.showLoading({
            title: '正在打印,请稍后',
            mask:true
          })
          wx.request({
            url: 'https://zujieba.com/api/Test2/print.html',
            data:{type:1,status:1,code:this.data.code},
            success:(res1)=>{
              console.log(this.data.code,this.data.package);
              
              if(res1.data.success==1){
                console.log(res1);
                
                wx.request({
                  url: 'https://zujieba.com/api/Test2/print.html',
                  data:{type:2,status:1,code:this.data.code},
                  success:(res)=>{
                    console.log(res);
                    
                    wx.hideLoading()
                    if(res.data.success==1){
                      
                    let task_id=res.data.data.data.task_id
                    let deviceId=res.data.data.order.deviceId
                    let deviceKey=res.data.data.order.deviceKey
                    this.getxunhuan(task_id,deviceId,deviceKey)
                  
                  
                  }else{wx.showToast({
                       title: res.data.tips,
                       icon:'none'
                     })
                    console.log(res1);
                    }
                  }})
                
              }else{
                wx.hideLoading(
                       
                  )
                wx.showToast({
                  title: res1.data.tips
                })
              }
              
            }
          })},1500)
       
           
    },
    download(){
        
    },
    navorder(){
     wx.navigateTo({
       url: '/pages/orderprint/orderprint?change=2'
     })
    },
    getxunhuan(task_id){
       setTimeout(()=>{
        wx.request({
          url: 'https://zujieba.com/api/Test2/print.html',
          
          data:{task_id,type:3,status:1,code:this.data.code},
          success:(res2)=>{
             console.log(res2);
             
            
            var title
            if(res2.data.data.data.task_state=='READY'){
               title='排队中'
            }else if(res2.data.data.data.task_state=='PARSING'){
               title='解析中'
            }else if(res2.data.data.data.task_state=='SENDING'){
              title='发送中'
            }else if(res2.data.data.data.task_state=='SUCCESS'){
              title='成功'
            }else if(res2.data.data.data.task_state=='FAILURE'){
              title='失败'
            }
            wx.showLoading({
              title: title,
            })
           
            if(res2.data.data.data.task_state=='SUCCESS'){
              this.setData({flag:true})
              
                wx.hideLoading()
                wx.navigateTo({
                  url: '/pages/orderprint/orderprint?change=3',
                })
             
             
            }else if(res2.data.data.data.task_state=='FAILUER'){
              wx.hideLoading()
              this.setData({flag:false})
            }else{
              this.getxunhuan(task_id)
            }
           
          }
        })
       },1000)
    },
    /*getorder(){
       wx.request({
         url: 'https://zujieba.com/Api/Test2/change_order',
         data:{code:this.data.code},
        method:'POST',
        success:(res)=>{
          console.log(res);
          if(res.data.success==1){
            wx.navigateTo({
              url: '/pages/orderprint/orderprint?change=3',
            })
          }
          
        }
       })
    },*/
    //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
    down(){
      console.log(111);
      
      wx.getImageInfo({
        src: this.data.url,
        success:(res)=>{
          console.log(res);
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success:(res1)=>{
              console.log(res1);
              
            }
          })
          
        }
      })
    }
 
})